package com.gotop.sms.fysms;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Transaction;

import com.app.Const;
import com.pioneer.app.dev.osuser.user.Userlist;
import com.pioneer.app.dev.osuser.user.UserlistDAO;

import config.SMSConfig;

public class BacterinService {
	
	public void userBacterinInit(Integer userId){
		BacterinDAO bdao=new BacterinDAO();
		UserBacterinDAO ubdao=new UserBacterinDAO();
		UserlistDAO userDao=new UserlistDAO();
		Userlist user=userDao.findById(userId);
		List bList=bdao.findByCondition(null);
		Iterator it=bList.iterator();
		Bacterin bvo=null;
		Bacterin parentvo=null;
		UserBacterin ubvo=null;
		UserBacterin pubvo=null;
		try {
			while(it.hasNext()){
				bvo=(Bacterin)it.next();
				ubvo=new UserBacterin();
				ubvo.setBacterinId(bvo.getId());
				ubvo.setUserId(userId);
				ubvo.setBacterinName(bvo.getName());
				Date pdate=null;
				if(bvo.getTargetBacterin().intValue()==-1){
					
					pdate=(Date)user.getBirthday().clone();
				}else{
					parentvo=bdao.findById(bvo.getTargetBacterin());
					List ublist=ubdao.findByCondition("userId="+userId+" and bacterinId="+bvo.getTargetBacterin());
					if(null!=ublist){
						pubvo=(UserBacterin)ublist.get(0);
					}
					
					if(null!=pubvo.getInjectTime()){
						pdate=(Date)pubvo.getInjectTime().clone();
					}else{
						pdate=(Date)pubvo.getOrderTime().clone();
					}
					if(Const.UNIT_MONTH.equalsIgnoreCase(bvo.getOffNumUnit())){
						pdate.setMonth(pdate.getMonth()+bvo.getOffNum());
						
					}else if(Const.UNIT_YEAR.equalsIgnoreCase(bvo.getOffNumUnit())){
						pdate.setYear(pdate.getYear()+bvo.getOffNum());
						
					}else if(Const.UNIT_WEEK.equalsIgnoreCase(bvo.getOffNumUnit())){
						
					}else if(Const.UNIT_DAY.equalsIgnoreCase(bvo.getOffNumUnit())){
						pdate.setDate(pdate.getDate()+bvo.getOffNum());
					}
					
				}
//			时间设置好了，保存到数据库中
				try {
					//System.out.println(pdate.toLocaleString());
					//System.out.println(pdate.getDay());
					if(pdate.getDay()>2)
						pdate=com.app.util.DateTimeUtil.toNextWeek(pdate, 5);
					else
						pdate=com.app.util.DateTimeUtil.toNextWeek(pdate, 2);
					ubvo.setOrderTime(pdate);
					if(bvo.getTargetBacterin().intValue()==-1){
						ubvo.setInjectTime(pdate);	
					}
					Date wdate=(Date)pdate.clone();
					wdate.setDate(wdate.getDate()-2);
					ubvo.setWeakTime(wdate);
					
					ubvo.setPhone(user.getTelphone());
					if(user.getKind()!=null && user.getKind().equals("固定")){
						ubvo.setState(1);
					}else{
						ubvo.setState(0);
					}
					Transaction tx=ubdao.getSession().beginTransaction();
					ubdao.add(ubvo);
					tx.commit();
				} catch (RuntimeException e) {
					e.printStackTrace();
				}
			}
			
		} catch (RuntimeException e) {
//			tx.rollback();
			e.printStackTrace();
		}
		
	}

	public UserBacterin getLastBacterin(Integer userId){
		UserBacterinDAO ubdao=new UserBacterinDAO();
		List ls=ubdao.findByCondition("userId="+userId+"");
		if(null!=ls && ls.size()>0){
			UserBacterin ub=(UserBacterin)ls.get(0);
			return ub;
		}
		return null;
	}

	public void userBacterinfresh(Integer userId){
		BacterinDAO bdao=new BacterinDAO();
		UserBacterinDAO ubdao=new UserBacterinDAO();
		UserlistDAO userDao=new UserlistDAO();
		Userlist user=userDao.findById(userId);
		List bList=bdao.findByCondition(null);
		Iterator it=bList.iterator();
		Bacterin bvo=null;
		Bacterin parentvo=null;
		UserBacterin ubvo=null;
		UserBacterin pubvo=null;
		try {
			while(it.hasNext()){
				bvo=(Bacterin)it.next();
				List ublist=ubdao.findByCondition("userId="+userId+" and bacterinId="+bvo.getId());
				ubvo=(UserBacterin)ublist.get(0);
				/**
					ubvo=new UserBacterin();
					ubvo.setBacterinId(bvo.getId());
					ubvo.setUserId(userId);
				**/
				Date pdate=null;
//				pubvo=ubdao.findById(bvo.getTargetBacterin());
				if(bvo.getTargetBacterin().intValue()==-1){
					//ubvo.setOrderTime(user.getBirthday());
					//ubvo.setInjectTime(user.getBirthday());
					pdate=(Date)user.getBirthday().clone();
				}else{
					parentvo=bdao.findById(bvo.getTargetBacterin());
					List publist=ubdao.findByCondition("userId="+userId+" and bacterinId="+bvo.getTargetBacterin());
					if(null!=publist){
						pubvo=(UserBacterin)publist.get(0);
					}
					
					if(null!=pubvo.getInjectTime()){
						pdate=(Date)pubvo.getInjectTime().clone();
					}else{
						pdate=(Date)pubvo.getOrderTime().clone();
					}
					if(Const.UNIT_MONTH.equalsIgnoreCase(bvo.getOffNumUnit())){
						pdate.setMonth(pdate.getMonth()+bvo.getOffNum());
						
					}else if(Const.UNIT_YEAR.equalsIgnoreCase(bvo.getOffNumUnit())){
						pdate.setYear(pdate.getYear()+bvo.getOffNum());
						
					}else if(Const.UNIT_WEEK.equalsIgnoreCase(bvo.getOffNumUnit())){
						
					}else if(Const.UNIT_DAY.equalsIgnoreCase(bvo.getOffNumUnit())){
						pdate.setDate(pdate.getDate()+bvo.getOffNum());
					}
					
				}
//			时间设置好了，保存到数据库中
				try {
					//System.out.println(pdate.toLocaleString());
					//System.out.println(pdate.getDay());
					if(pdate.getDay()>2)
						pdate=com.app.util.DateTimeUtil.toNextWeek(pdate, 5);
					else
						pdate=com.app.util.DateTimeUtil.toNextWeek(pdate, 2);
					ubvo.setOrderTime(pdate);
					Date wdate=(Date)pdate.clone();
					wdate.setDate(wdate.getDate()-Integer.parseInt(SMSConfig.getInst().getFYWeakBeforeNum()));
					ubvo.setWeakTime(wdate);
//					ubvo.setInjectTime(pdate);
					Transaction tx=ubdao.getSession().beginTransaction();
					ubdao.update(ubvo);
					tx.commit();
				} catch (RuntimeException e) {
					e.printStackTrace();
				}
			}
			
		} catch (RuntimeException e) {
//			tx.rollback();
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args){
		BacterinService bs=new BacterinService();
		//bs.userBacterinInit(new Integer(1));
		//Date now=new Date();
		//System.out.println(now.toLocaleString());
		//now.setYear(now.getYear()+3);
		//System.out.println(now.toLocaleString());
		UserBacterin ub=bs.getLastBacterin(new Integer(5));
		System.out.println(ub.getOrderTime());
	}
}
